

package com.asisto.controller
import com.asisto.model.Empleado

class EmpleadoController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ empleadoInstanceList: Empleado.list( params ), empleadoInstanceTotal: Empleado.count() ]
    }

    def show = {
        def empleadoInstance = Empleado.get( params.id )

        if(!empleadoInstance) {
            flash.message = "Empleado not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ empleadoInstance : empleadoInstance ] }
    }

    def delete = {
        def empleadoInstance = Empleado.get( params.id )
        if(empleadoInstance) {
            try {
                empleadoInstance.delete(flush:true)
                flash.message = "Empleado ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Empleado ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Empleado not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def empleadoInstance = Empleado.get( params.id )

        if(!empleadoInstance) {
            flash.message = "Empleado not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ empleadoInstance : empleadoInstance ]
        }
    }

    def update = {
        def empleadoInstance = Empleado.get( params.id )
        if(empleadoInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(empleadoInstance.version > version) {
                    
                    empleadoInstance.errors.rejectValue("version", "empleado.optimistic.locking.failure", "Another user has updated this Empleado while you were editing.")
                    render(view:'edit',model:[empleadoInstance:empleadoInstance])
                    return
                }
            }
            empleadoInstance.properties = params
            if(!empleadoInstance.hasErrors() && empleadoInstance.save()) {
                flash.message = "Empleado ${params.id} updated"
                redirect(action:show,id:empleadoInstance.id)
            }
            else {
                render(view:'edit',model:[empleadoInstance:empleadoInstance])
            }
        }
        else {
            flash.message = "Empleado not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def empleadoInstance = new Empleado()
        empleadoInstance.properties = params
        return ['empleadoInstance':empleadoInstance]
    }

    def save = {
        def empleadoInstance = new Empleado(params)
        if(!empleadoInstance.hasErrors() && empleadoInstance.save()) {
            flash.message = "Empleado ${empleadoInstance.id} created"
            redirect(action:show,id:empleadoInstance.id)
        }
        else {
            render(view:'create',model:[empleadoInstance:empleadoInstance])
        }
    }

    def muestraFoto = {

        def empleado = Empleado.findById(params.id)

        if (empleado?.foto) {
            response.setContentLength(empleado.foto.length)
            response.outputStream.write(empleado.foto)
            
        } else {
            response.sendError(404)
        }
    }
}
